package com.geeksaga.dao.ibatis.mysql;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import com.geeksaga.common.util.DateConvertor;
import com.geeksaga.dao.ProjectMemberDao;
import com.geeksaga.dao.ibatis.BaseIBatisDao;
import com.geeksaga.domain.Project;
import com.geeksaga.domain.ProjectMember;

/**
 * @author newbie
 * @since 0.1
 */
@Repository
public class ProjectMemberDaoImpl extends BaseIBatisDao implements ProjectMemberDao {

    /* (non-Javadoc)
     * @see com.geeksaga.dao.ProjectMemberDao#add(com.geeksaga.domain.ProjectMember)
     */
    public ProjectMember add(ProjectMember member) throws DataAccessException {
        member.setRegistTimestamp(DateConvertor.getDateTimeFormat());
        
        return (ProjectMember) getSqlMapClientTemplate().insert("PROJECT_MEMBER.add", member);
    }
    
    /* (non-Javadoc)
     * @see com.geeksaga.dao.ProjectMemberDao#findListByProject(com.geeksaga.domain.Project)
     */
    @SuppressWarnings("unchecked")
	public List<ProjectMember> findListByProject(Project project) throws DataAccessException {
    	return (List<ProjectMember>) getSqlMapClientTemplate().queryForList("PROJECT_MEMBER.findByProject", project);
    }

    /* (non-Javadoc)
     * @see com.geeksaga.dao.ProjectMemberDao#findByProject(com.geeksaga.domain.Project)
     */
    @SuppressWarnings("unchecked")
    public Map<String, String> findByProject(Project project) throws DataAccessException {
        Map<String, String> map = new HashMap<String, String>();
		
        List<ProjectMember> list = (List<ProjectMember>) getSqlMapClientTemplate().queryForList("PROJECT_MEMBER.findByProject", project);
        
        if(list != null) {
	        Iterator<ProjectMember> iterator = list.iterator();
	        
	        while(iterator.hasNext()) {
	        	ProjectMember manager = iterator.next();
	            map.put(manager.getUserSid(), manager.getProjectCode());
	        }
        }
        
        return map;
    }
}